package mirkofrancuski.battleship.db;

import mirkofrancuski.battleship.game.Board;
import mirkofrancuski.battleship.game.Cell;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class BoardTable {
	
	public static String BOARD_PLACEMENT = "Placement_Board";
	public static String BOARD_GAME_PLAYER_MAIN = "Main_Game_Player_Board";
	public static String BOARD_GAME_PLAYER_SECUNDARY = "Secundary_Game_Player_Board";
	public static String BOARD_GAME_COMPUTER_MAIN = "Main_Game_Computer_Board";
	public static String BOARD_GAME_COMPUTER_SECUNDARY = "Secundary_Game_Computer_Board";
	
	private static String DEBUG_TAG = "DEBUG BoardTable";

	private static String BOARD_TABLE = "";
	private static final String BOARD_TABLE_ROWS = "Rows";
	private static final String ROW = "row";

	public static void onCreate(SQLiteDatabase database, String tableName) {
		BOARD_TABLE = tableName;
				
		String createTable = "CREATE TABLE " + BOARD_TABLE + 
				" ("
				+ BOARD_TABLE_ROWS + " INTEGER PRIMARY KEY, ";

		for (int i = 0; i < Board.BOARD_SIZE; i++)
			createTable += ROW + Integer.toString(i) + " INTEGER"
					+ (i != Board.BOARD_SIZE -1 ? ", " : "");
		createTable += ")";

		database.execSQL(createTable);
	}

	public static void setTableName(String tableName) {
		BOARD_TABLE = tableName;
	}

	public static void onUpgrade(SQLiteDatabase database, int oldVersion,
			int newVersion, String tableName) {
		Log.w(HighScoreTable.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		database.execSQL("DROP TABLE IF EXISTS " + tableName);
		onCreate(database, tableName);
	}

	public static void saveBoard(SQLiteDatabase db, Board board, String tableName) {
		deleteBoard(db, tableName);
		ContentValues cv;
		for (int i = 0; i < Board.BOARD_SIZE; i++) {
			cv = new ContentValues();
			cv.put(BOARD_TABLE_ROWS, i);
			for (int j = 0; j < Board.BOARD_SIZE; j++) {
				cv.put(ROW + j, board.getCell(i, j).ordinal());
			}
			db.insert(tableName, null, cv);
		}
		Log.d(DEBUG_TAG, "Matrix saved: \n"  + readMatrix(db, tableName).toString());
	}

	public static void deleteBoard(SQLiteDatabase db, String tableName) {
		db.delete(tableName, null, null);		
	}

	public static Board restoreBoard(SQLiteDatabase db, String tableName) {
		int[][] matrix = readMatrix(db, tableName);

		if(matrix == null)
			return null;
		
		Log.d(DEBUG_TAG, "Matrix read: \n"  + readMatrix(db, tableName).toString());
		
		Board board = new Board();
		board.setBoard(matrix);
		Log.d(DEBUG_TAG, "Board read: \n"  + board.toString());
		return board;
	}
	
	private static int[][] readMatrix(SQLiteDatabase db, String tableName){
		int[][] matrix = null;

		String selectQuery = "SELECT * FROM " + tableName;
		Cursor cursor = db.rawQuery(selectQuery, null);
		
		
		for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){	
			if(matrix == null)
				matrix = new int[Board.BOARD_SIZE][Board.BOARD_SIZE];
			int i = Integer.parseInt(cursor.getString(0));
			for (int j = 0; j < Board.BOARD_SIZE; j++){
				
				String number = cursor.getString(j + 1);
				//number = number.substring(ROW.length());
				matrix[i][j] =Integer.parseInt(number); 
			}

		}
		return matrix;
	}
}
